package com.guchenbo.example.thread;

import com.guchenbo.example.lock.WaitClientCloseServer;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ThreadDemo {

    private static ExecutorService executor = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ThreadDemo demo = new ThreadDemo();
        // 1、子线程为非守护线程，不会退出
//                demo.test1();
        // 2、子线程为守护线程，直接退出
//        demo.test2();
        // 3、子线程为守护线程，但是主线程调用子线程join之后，等待子线程结束再退出
//        demo.test3();

        Thread t1 = GcbThreadFactory.create("t1", new ErrorTask());
        System.out.println(t1.getUncaughtExceptionHandler());
//        t1.start();

//        executor.execute(new ErrorTask());
        Future future = executor.submit(new ErrorTask());
//        while (!future.isDone()) {
//            System.out.println("not done");
////            System.out.println(future.isCancelled());
//        }
        System.out.println(future.get());
        System.out.println(future.isDone());
        //        System.out.println(future);
//        System.out.println(future.get());
//        System.out.println(future.isDone());
//        System.out.println("dd");



    }

    static class ErrorTask implements Runnable{

        @Override
        public void run() {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            new WaitClientCloseServer().start(10000);
            System.out.println(Integer.parseInt("s"));
        }
    }

    public void test1() {
        System.out.println("main start");
        Thread child = new Thread(() -> {
            try {
                System.out.println("child start");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println("child end");
            }
        });
        child.start();
        System.out.println("main end");
    }

    public void test2() {
        System.out.println("main start");
        Thread child = new Thread(() -> {
            try {
                System.out.println("child start");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println("child end");
            }
        });
        child.setDaemon(true);
        child.start();
        System.out.println("main end");
    }

    public void test3() throws InterruptedException {
        System.out.println("main start");
        Thread child = new Thread(() -> {
            try {
                System.out.println("child start");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println("child end");
            }
        });
        child.setDaemon(true);
        child.start();
        child.join();
        System.out.println("main end");
    }
}
